//
//  HZMingXiView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/15.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZMingXiView.h"

#define kLineW 0.5
#define kWuDangMargin 5

@interface HZMingXiView()
@property (nonatomic, strong)UIView *pVerticalLine;
@property (nonatomic, strong)NSMutableArray *pTimeArray;
@property (nonatomic, strong)NSMutableArray *pPriceArray;
@property (nonatomic, strong)NSMutableArray *pNumArray;
@end

@implementation HZMingXiView
- (NSMutableArray *)pTimeArray
{
    if (!_pTimeArray) {
        _pTimeArray = [NSMutableArray arrayWithCapacity:20];
    }
    return _pTimeArray;
}
- (NSMutableArray *)pPriceArray
{
    if (!_pPriceArray) {
        _pPriceArray = [NSMutableArray arrayWithCapacity:20];
    }
    return _pPriceArray;
}
- (NSMutableArray *)pNumArray
{
    if (!_pNumArray) {
        _pNumArray = [NSMutableArray arrayWithCapacity:20];
    }
    return _pNumArray;
}
#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [HZCommonTool getThemeColor];
    }
    return self;
}

- (void)layoutSubviews{
    _pVerticalLine.center = CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)/2);
    _pVerticalLine.bounds = CGRectMake(0, 0, kLineW, CGRectGetHeight(self.frame));
    
    CGFloat fItemW = (CGRectGetWidth(self.frame) - 4*kWuDangMargin - kLineW)/6;
    CGFloat fItemH = CGRectGetHeight(self.frame)/5;
    for (int i = 0; i < _pTimeArray.count; i++) {
        UILabel *pTime = _pTimeArray[i];
        UILabel *pPrice = _pPriceArray[i];
        UILabel *pNum = _pNumArray[i];
        CGFloat Y = fItemH * (i%5);
        CGFloat timeX = kWuDangMargin + (CGRectGetMaxX(_pVerticalLine.frame)) * (i/5);
        CGFloat priceX = kWuDangMargin + fItemW + (CGRectGetMaxX(_pVerticalLine.frame)) * (i/5);
        CGFloat numX = kWuDangMargin + 2*fItemW + (CGRectGetMaxX(_pVerticalLine.frame)) * (i/5);
        pTime.frame = CGRectMake(timeX, Y, fItemW, fItemH);
        pPrice.frame = CGRectMake(priceX, Y, fItemW, fItemH);
        pNum.frame = CGRectMake(numX, Y, fItemW, fItemH);
    }
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pVerticalLine = [UIView new];
    _pVerticalLine.backgroundColor = [HZCommonTool getThemeSeperateColorWithDefaultColor:nil];
    [self addSubview:_pVerticalLine];
   
    for (int i = 0; i < 10; i++) {
        UILabel *pTime = [UILabel new];
        UILabel *pPrice = [UILabel new];
        UILabel *pNum = [UILabel new];
        pTime.text = kDefaultContent;
        pPrice.text = kDefaultContent;
        pPrice.textAlignment = NSTextAlignmentRight;
        pNum.text = kDefaultContent;
        pNum.textAlignment = NSTextAlignmentCenter;
        pTime.backgroundColor = [UIColor clearColor];
        pPrice.backgroundColor = [UIColor clearColor];
        pNum.backgroundColor = [UIColor clearColor];
        pTime.textColor = kMYHexToRGB(0x6b6b6b, 1);
        pTime.font = pPrice.font = pNum.font = [UIFont systemFontOfSize:11];
        [self addSubview:pTime];
        [self addSubview:pPrice];
        [self addSubview:pNum];
        [self.pTimeArray addObject:pTime];
        [self.pPriceArray addObject:pPrice];
        [self.pNumArray addObject:pNum];
        
        pTime.adjustsFontSizeToFitWidth = YES;
        pPrice.adjustsFontSizeToFitWidth = YES;
        pNum.adjustsFontSizeToFitWidth = YES;
    }
}
#pragma mark -
#pragma mark public
- (void)updateNewData:(NSArray *)data
{
    if (data) {
        for (int i = 0; i < data.count; i++) {
            if (i > 9) {
                return;
            }
            HZMingXiModel *model = data[i];
            UILabel *pTimeLabel = _pTimeArray[i];
            UILabel *pPriceLabel = _pPriceArray[i];
            UILabel *pNumLabel = _pNumArray[i];
            pTimeLabel.text = model.model_time;
            pPriceLabel.text = model.model_price;
            pNumLabel.text = model.model_num;
            pPriceLabel.textColor = model.model_priceColor;
            pNumLabel.textColor = model.model_numColor;
        }
    }
}

@end
